package oj;

import java.util.LinkedList;
import java.util.List;

public class LC239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums==null || nums.length<2) return nums;
        LinkedList<Integer> list=new LinkedList<>();
        int[] arr=new int[nums.length-k+1];
        for(int i=0;i<nums.length;i++){
            while(!list.isEmpty() && nums[list.peekLast()]<=nums[i]){
                list.pollLast();
            }
            list.addLast(i);
            if(list.peek()<=i-k){
                list.poll();
            }
            if(i+1>=k){
                arr[i-k+1]=nums[list.peek()];
            }
        }
        return arr;
    }
}
